package hotel.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javafx.scene.control.Alert;
import javafx.scene.control.Alert.*;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ChoiceDialog;
import javafx.scene.control.TextInputDialog;

public class MsgBox {
	
	// 正常消息提示框
	public static void showInformationDialog(String title, String head, String content) {
		MsgBox.showSimpleDialog(title, head, content, AlertType.INFORMATION);
	}
	
	// 错误消息提示框
	public static void showErrorDialog(String title, String head, String content) {
		MsgBox.showSimpleDialog(title, head, content, AlertType.ERROR);
	}
	
	// 警告消息提示框
	public static void showWarningDialog(String title, String head, String content) {
		MsgBox.showSimpleDialog(title, head, content, AlertType.WARNING);
	}
	
	// 确认消息提示框
	public static Boolean showConfirmationDialog(String title, String head, String content) {
		Alert alert = new Alert(AlertType.CONFIRMATION);
		alert.setTitle(title);
		if(head != null && !head.equals("")) 
			alert.setHeaderText(head);
		else 
			alert.setHeaderText(null);
		alert.setContentText(content);

		Optional<ButtonType> result = alert.showAndWait();
		
		if (result.get() == ButtonType.OK) {
		    return true;
		} else {
		    return false;
		}
	}
	
	// 文本输入消息提示框
	public static String showInputDialog(String title, String head, String content, String defaultValue) {
		TextInputDialog dialog = new TextInputDialog(defaultValue);
		dialog.setTitle(title);
		if(head != null && !head.equals("")) 
			dialog.setHeaderText(null);
		else
			dialog.setHeaderText(head);
		dialog.setContentText(content);

		// Traditional way to get the response value.
		Optional<String> result = dialog.showAndWait();
		if (result.isPresent()){
			return result.get();
		}
		return null;
		// The Java 8 way to get the response value (with lambda expression).
		//result.ifPresent(name -> System.out.println("Your name: " + name));
	}
	
	// 选择框消息提示框
	public static String showChoiceDialog(String title, String head, String content, ArrayList<String> list, String defaultValue) {
		List<String> choices = new ArrayList<>(list);
		
		ChoiceDialog<String> dialog = new ChoiceDialog<>(defaultValue, choices);
		dialog.setTitle(title);
		dialog.setHeaderText(head);
		dialog.setContentText(content);

		// Traditional way to get the response value.
		Optional<String> result = dialog.showAndWait();
		if (result.isPresent()){
			return result.get();
		}
		return null;
	}
	
	public static void showSimpleDialog(String title, String head, String content, AlertType type) {
		Alert alert = new Alert(type);
		alert.setTitle(title);
		if(head != null && !head.equals("")) alert.setHeaderText(head);
		else alert.setHeaderText(null);
		alert.setContentText(content);
		alert.showAndWait();
	}
}
